#include "cswitch.h"
#include "cmd_parse/json_device_object.h"

CSwitch::CSwitch(CKnxControl *parent) :
    CKnxControl(parent)
{
    _device_name = "灯";
}

void CSwitch::execDeviceAction(QString action,QString value,QString unit)
{
    execKnxAction(action,value);
}

void CSwitch::setKnxObject(void *jOBJECT)
{
    //表达式状态
    _onValue = "1";
    _offValue = "0";

    Json_DEVICE_OBJECT *object = (Json_DEVICE_OBJECT *)jOBJECT;

    QStringList arrys;
    object->_exp = object->_exp.trimmed();

    if(!object->_exp.contains("location"))
    {
        if(!object->_exp.isEmpty())
        {
            arrys = object->_exp.split(";");
            if(arrys.size()!=0)
            {
                for(int i = 0;i<arrys.size();i++)
                {
                    if (i == 0)
                    {
                        _onValue = arrys.at(i);
                    }
                    else if (i == 1)
                    {
                        _offValue = arrys.at(i);
                    }
                }
            }
        }
    }
    else
    {
        QString _onValue_msg = foundKeyFromExp(object->_exp,"on");
        if(!_onValue_msg.isEmpty())
        {
            _onValue = _onValue_msg;
        }

        QString _offValue_msg = foundKeyFromExp(object->_exp,"off");
        if(!_offValue_msg.isEmpty())
        {
            _offValue = _offValue_msg;
        }
    }

    //发送值
    arrys = object->_send.split(";");
    if(arrys.size()>1)
    {
        QString sonArray = arrys.at(0);
        _sendOnArray = sonArray.split(",");

        QString soffArray = arrys.at(1);
        _sendOffArray = soffArray.split(",");
    }

    //反馈值
    arrys = object->_feedback.split(":");
    if(arrys.size() >= 3)
    {
        _feedbackAddress = arrys.at(0);
        _feedbackDataType =  (DATA_TYPE)arrys.at(1).toUInt();
        _feedbackValueDefine =  (DATA_DEFINE)arrys.at(2).toUInt();
    }

}

void CSwitch::readKnxAction()
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress;

    eThread->execList.append(s);

    eThread->start();
}

void CSwitch::execKnxAction(QString action,QString value)
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    bool currState = true;
    QString onOffValue = _onValue;
    QStringList *sendArray = &_sendOnArray;

    if (action == "off") {
        currState = false;
        onOffValue = _offValue;
        sendArray = &_sendOffArray;
    };

    QString sendAddress,sendValue;
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() >= 3) {
            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {
                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();

                if(devArray.size() > 3)
                {
                    sendValue = devArray.at(3);
                }

                if(!sendAddress.isEmpty())
                {
                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }

    }

    eThread->start();

}

QString CSwitch::actionFromStatus(QString address,QString value)
{
    if(value == _onValue)
        return "on";
    else
        return "off";
}
